package com.bafa.services.teams;

import com.bafa.jdbc.DBAccess;
import com.bafa.jdbc.QueryObject;
import com.bafa.jdbc.UpdateObject;
import com.bafa.objects.team.Team;
import com.bafa.services.RetrieveServices;



public class TeamServices {
/*
	static void addTeam(String name,
								  String contactName1,
								  String contactName2,
								  String contactEmail1,
								  String contactEmail2,
								  String contactTel1,
								  String contactTel2,
								  String shirtColour,DBAccess db)throws Exception{
		
		Message message = new Message();
		QueryObject query = new QueryObject(Keys.TEAMS);
		
		UpdateObject update = new UpdateObject();
		update.addField(Keys.TEAMS_Name,name);
		
		if(contactName1!=null)update.addField(Keys.TEAMS_ContactName1,contactName1);
		if(contactName2!=null)update.addField(Keys.TEAMS_ContactName2,contactName2);
		if(contactEmail1!=null)update.addField(Keys.TEAMS_ContactEmail1,contactEmail1);
		if(contactEmail2!=null)update.addField(Keys.TEAMS_ContactEmail2,contactEmail2);
		if(contactTel1!=null)update.addField(Keys.TEAMS_ContactTel1,contactTel1);
		if(contactTel2!=null)update.addField(Keys.TEAMS_ContactTel2,contactTel2);
		if(shirtColour!=null)update.addField("ShirtColour",shirtColour);
		
		
		ResultSet newTeam = db.insert(query,update);
	    int teamId = 0;
	    while(newTeam.next()){
                teamId = newTeam.getInt("ID");
            }
	    ObjectFactory factory=new ObjectFactory();
	    Team team = factory.createTeam();
	    TeamDetails teamDetails = factory.createTeamDetails();
	    teamDetails.setID(teamId);
	    teamDetails.setName(name);
	    teamDetails.setContactName1(contactName1);
	    teamDetails.setContactName2(contactName2);
	    teamDetails.setContactEmail1(contactEmail1);
	    teamDetails.setContactEmail2(contactEmail2);
	    teamDetails.setContactTel1(contactTel1);
	    teamDetails.setContactTel2(contactTel2);
	    teamDetails.setHomeShirtColour(shirtColour);
	    teamDetails.setPaymentRequired(0.0);
	    
	    team.setTeamDetails(teamDetails);
	    TeamStats stats = GrabTeamStats.grabTeamStats(db,team);
            
	    team.setTeamStats(stats);
	    GrabTeams.addTeam(team);
	    db.storeTeam(team);
	    
	}
*/
	public static int updateTeamDetails(String teamId, String newContactName1, String newContactName2, String tel1, String tel2, String email1, String email2, String newShirtColour, String newuser){
		DBAccess db = new DBAccess();
		
		Team team = RetrieveServices.getTeam(teamId);
		if(newContactName1!=null&&!newContactName1.equals(""))team.setPrimaryContactName(newContactName1);
		if(newContactName2!=null&&!newContactName2.equals(""))team.setSecondaryContactName(newContactName2);
		if(newShirtColour!=null&&!newShirtColour.equals(""))team.setShirtColour(newShirtColour);
		if(email1!=null&&!email1.equals(""))team.setPrimaryContactEmail(email1);
		if(email2!=null&&!email2.equals(""))team.setSecondaryContactEmail(email2);
		if(tel1!=null)team.setPrimaryContactTel(tel1);
		if(tel2!=null)team.setSecondaryContactTel(tel2);
		QueryObject queryUsers = new QueryObject("Users");
		queryUsers.addWhereAndField("TeamID",teamId);
		UpdateObject updateUsers = new UpdateObject();
		if(newuser!=null&&!newuser.equals(""))updateUsers.addField("ID",newuser);
		
		
		try{
			int figure = 3;
			db.updateTeam(team);
			if(updateUsers.hasNext()){
				db.update(queryUsers,updateUsers);
				figure=0;
			}
		
			
			
			return figure;
		}catch(Exception e){
			e.printStackTrace();
			if(e.getMessage().indexOf("Duplicate")>-1){
				return 2;
			}
			return 1;
		}finally{
			db.disconnect();
		}
	
		
	}
}
